package tools

import com.huawei.ais.common.AuthInfo
import com.huawei.ais.sdk.AisAccess
import com.huawei.ais.sdk.util.HttpClientUtils
import exceptions.ExternalException
import org.apache.http.HttpResponse
import org.apache.http.entity.StringEntity

fun main() {
    val strings = arrayListOf("还不给爷滚", "日你妹的", "美女外围+V 156 614O 74I3","什么傻软","Hello World")

    for (string in strings) {
        println(ContentCheck.check(string).toString() + "\t" + string)
    }
}

object ContentCheck {

    private val keywords = arrayListOf(
        "毛泽东",
        "邓小平",
        "江泽民",
        "胡锦涛",
        "习近平",
        "拜登",
        "特朗普",
        "川普",
        "林彪",
        "江青",
        "四人帮",
        "文革",
        "文蛤",
        "文化大革命",
        "wg",
        "sb",
        "六四",
        "天安门",
        "傻逼",
        "傻卵",
        "沙雕",
        "肏",
        "屌",
        "鸡巴",
        "烧鸡",
        "骚鸡",
        "小穴",
        "肛",
        "屄",
        "cnm",
        "你妈",
        "你马",
        "吸毒",
        "嫖娼",
        "假唱",
        "弱智",
        "脑瘫",
        "人渣",
        "蛆",
        "国蝻",
        "郭楠",
        "蝈蝻",
        "癌",
        "中国特色",
        "wx",
        "w我",
        "婊",
        "潮牌",
        "电子烟",
        "共青团",
        "共产党",
        "二十大",
        "20大",
        "贱",
        "挂人",
        "垃圾",
        "辣鸡",
        "勾八",
        "两岸三地",
        "两岸四地",
        "一中各表",
        "台独",
        "兩岸三地",
        "兩岸四地",
        "台獨",
        "台聯"
    )

    /**
     * 对投递的漂流瓶进行内容审核
     */
    @JvmStatic
    fun check(content: String): Boolean {
        return normalKeywordsCheck(content) && huaweiCheck(content)
    }

    /**
     * 简易的屏蔽词审核
     */
    @JvmStatic
    private fun normalKeywordsCheck(content: String): Boolean {
        val lowerContent = content.lowercase().replace(" ","")
        for (keyword in keywords) {
            if (lowerContent.contains(keyword)) {
                return false
            }
        }
        return true
    }

    /**
     * 华为云屏蔽词审核
     */
    @JvmStatic
    private fun huaweiCheck(content: String): Boolean {
        // 1. 配置好访问文本内容检测服务的基本信息,生成对应的一个客户端连接对象

        val authInfo = AuthInfo(
            "https://moderation.cn-east-3.myhuaweicloud.com",
            "cn-east-3",
            "ZN3KO231QLC4B9AJP3OU",
            "TVUhh7RQ7x4qIBN9T9gyuKcO7nKPvXL4hmsn536F"
        )
        val service = AisAccess(
            authInfo,
            1000,
            2000,
            5000,
            1
        )

        try {
            // 2.构建访问文本内容检测服务需要的参数
            val uri = "/v1.0/moderation/text"
            val json =
                "{\"categories\":[\"porn\",\"politics\",\"ad\",\"abuse\",\"contraband\",\"flood\"],\"items\":[{\"text\":\"${content}\",\"type\":\"content\"}]}"

            // 3.传入文本内容检测服务对应的uri参数, 传入文本内容检测服务需要的参数，
            // 该参数主要通过JSON对象的方式传入, 使用POST方法调用服务
            val response: HttpResponse = service.post(uri, StringEntity(json, "UTF-8"))

            // 4.验证服务调用返回的状态是否成功，如果为200, 为成功, 否则失败。
            return if (response.statusLine.statusCode == 200) {
                // 5.处理服务返回的字符流，输出识别结果。
                val responseString = HttpClientUtils.convertStreamToString(response.entity.content)
                responseString.contains("\"suggestion\":\"pass\"")
            } else {
                System.err.println("华为云审核失效")
                false
            }
        } catch (e: Exception) {
            System.err.println("华为云审核发生异常")
            e.printStackTrace()
            throw ExternalException()
        }
    }
}